<template>
  <Form class="xtx-form" ref="formCom"  v-slot="{errors}" :validation-schema="veeValidateSchema">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field v-model='form.account' :class="{error:errors.accountApi}"  name='accountApi' class="input" type="text" placeholder="请输入用户名" />
      </div>
       <div v-if="errors.accountApi" class="error">{{errors.accountApi}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field v-model='form.mobile' :class="{error:errors.mobile}" name='mobile' class="input" type="text" placeholder="请输入手机号" />
      </div>
       <div v-if="errors.mobile" class="error">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field v-model='form.code' :class="{error:errors.code}" name='code' class="input" type="text" placeholder="请输入验证码" />
         <span class="code" @click="send">{{number===0?'发送验证码':`${number}秒后发送`}}</span>
      </div>
       <div v-if="errors.code" class="error">{{errors.code}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field v-model='form.password' :class="{error:errors.password}" name='password' class="input" type="password" placeholder="请输入密码" />
      </div>
       <div v-if="errors.password" class="error">{{errors.password}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field  :class="{error:errors.repassword}" name='repassword' class="input" type="password" placeholder="请确认密码" />
      </div>
       <div v-if="errors.repassword" class="error">{{errors.repassword}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="loginBtn">立即提交</a>
  </Form>
</template>

<script>
import veeValidateSchema from '@/utils/vee-validate-schema'
import { Form, Field } from 'vee-validate'
import { reactive, watch } from 'vue'
import Message from '@/components/library/Message'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { userTimer } from '@/hooks'
export default {
  name: 'CallbackPatch',
  components: {
    Form, Field
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const store = useStore()
    const router = useRouter()
    // 表单数据对象
    const form = reactive({
      unionId: '',
      account: null,
      mobile: null,
      code: null,
      password: null
    })
    // 定时器
    const { number, formCom, send } = userTimer(() => userQQPatchCode(form.mobile), form)
    // 监听unionId变化
    watch(() => props.unionId, () => {
      form.unionId = props.unionId
    }, { immediate: true })

    const loginBtn = async () => {
      // 校验所有表单
      const vaild = await formCom.value.validate()
      if (!vaild) return Message({ type: 'error', message: '表单校验失败' })

      try {
        const { result } = await userQQPatchLogin(form)
        console.log(result)
        // 将信息保存到本地
        store.commit('user/setUser', result)
        const redirectUrl = router.currentRoute.value.query.redirectUrl
        router.push(redirectUrl || '/')
        Message({ type: 'success', message: '登录成功' })
      } catch (error) {
        Message({ type: 'error', message: error.response.data.message })
      }
    }

    return { veeValidateSchema, form, send, formCom, number, loginBtn }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
